import os
import re

def batch_rename(directory, start_str, end_str, replacement):
    """
    批量重命名指定目录下的文件，替换文件名中以start_str开头并以end_str结尾的子字符串为replacement。
    
    :param directory: 文件所在的目录路径
    :param start_str: 需要替换的子字符串的开头
    :param end_str: 需要替换的子字符串的结尾
    :param replacement: 替换后的新字符串
    """
    # 构建正则表达式模式
    pattern = re.escape(start_str) + '(.*?)' + re.escape(end_str)
    
    # 遍历目录中的所有文件
    for filename in os.listdir(directory):
        # 使用正则表达式查找匹配的部分
        match = re.search(pattern, filename)
        if match:
            # 构建新的文件名
            new_filename = filename.replace(match.group(0), start_str + replacement + end_str)
            # 重命名文件
            os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
            print(f'Renamed: {filename} -> {new_filename}')

# 示例调用
directory = 'd:\\work\\python\\change_name\\test_dir'
start_str = 'old_'
end_str = '_end'
replacement = 'new'
batch_rename(directory, start_str, end_str, replacement)